package top.songxs.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import top.songxs.base.BaseDao;
import top.songxs.base.BaseServiceImpl;
import top.songxs.dao.CommunityDao;
import top.songxs.dao.DictDao;
import top.songxs.entity.Community;
import top.songxs.entity.Dict;
import top.songxs.service.CommunityService;
import top.songxs.util.CastUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CommunityService.class)
@Transactional
public class CommunityServiceImpl extends BaseServiceImpl<Community> implements CommunityService {
    @Autowired
    private CommunityDao communityDao;
    @Autowired
    private DictDao dictDao;

    @Override
    public List<Community> findAll() {
        return this.communityDao.findAll();
    }

    @Override
    protected BaseDao<Community> getEntityDao() {
        return communityDao;
    }

//    @Override
//    public PageInfo<Community> findPage(Map<String, Object> filters) {
//        //1.启用分页插件
//        int pageNum = CastUtil.castInt(filters.get("pageNum"));
//        int pageSize = CastUtil.castInt(filters.get("pageSize"));
//        PageHelper.startPage(pageNum,pageSize);
//        //2.查询数据库获取分页查询的结果
//        Page<Community> page = this.getEntityDao().findPage(filters);
//        //3.根据areaId、plateId获取areaName，plateName
//        List<Community> communityList = page.getResult();
//        for(Community community:communityList){
//            Dict dict = this.dictDao.getById(community.getAreaId());
//            community.setAreaName(dict.getName());
//            dict = this.dictDao.getById(community.getPlateId());
//            community.setPlateName(dict.getName());
//        }
//        //4.Page转换为PageInfo并返回
//        return new PageInfo<>(page.getResult(),10);
//    }


    @Override
    public Community getById(Serializable id) {
        Community community = super.getById(id);
         Dict dict = this.dictDao.getById(community.getAreaId());
         community.setAreaName(dict.getName());
         String plateName = this.dictDao.getNameById(community.getPlateId());
         community.setPlateName(plateName);
        return community;
    }
}
